####
# Author: M. Kenwick, S. Lee, B. Kolcak
# Purpose: Analysis of Congressional Votes
# Date: June 2, 2025 
####
rm(list=ls())

library(dplyr)
library(stringr)
library(tidytext)
library(tidyr)
library(quanteda) 
library(readxl)
library(sf)
library(lubridate)
library(readxl)
library(ggplot2)
library(stargazer)
library(pROC)

#Set working directory to top level of replication folder
setwd("~/Dropbox/cmr_cong/replication/")

#Load core data sets and transform variables
speech <- read.csv('master.csv')
key <- read.csv('members.csv')
key <- key %>%
  dplyr::select(speakerid, icpsr) %>%
  unique()
meta <- read.delim("fullmeta_vet.txt", header = TRUE, sep = "|", quote = "")
head(meta)
meta <- dplyr::select(meta, cong, speakerid,chamber, lastname, gender, party, 
                      military_collapse, combat) %>% unique()
meta$session <- as.numeric(substr(as.character(meta$speakerid),1,3))
speech <- speech %>% filter(keyword_count_iraq >0)
speech$tone <- (speech$positive - speech$negative) / speech$keyword_count 

############################# 1. House Joint Resolution 114: 2002-10-10. Authorization (Table 1)   ----

# -------------------------------------------------- #
#Part 1: Load Vote Choice Data 
#Source: VoteView (https://voteview.com/rollcall/RH1070961)
# -------------------------------------------------- #

house <- read_xls('votechoice/iraq_auth_house.xls')
senate <- read_xls('votechoice/iraq_auth_senate.xls')
auth <- rbind(house,senate)
auth$auth_yea <- ifelse(auth$V1==1,1,0)
auth$auth_nay <- ifelse(auth$V1==6,1,0)
auth <- dplyr::select(auth,icpsr,auth_yea,auth_nay)


# -------------------------------------------------- #
#Part 2: Sort Speech Data 
#Prior 6 months to the vote
#RH1070961 voted on 2002-10-10
# -------------------------------------------------- #

####
#2-1: Filter Speech
####

speech$date<-as_date(speech$date)
prior <- speech %>% filter(date >= as.Date("2002-04-10") & date < as.Date("2002-10-10"))

####
#2-2: Average Tone by Members 
####

prior_ppl <- prior %>% 
  group_by(speakerid) %>% 
  mutate(tone = mean(tone,na.rm=T), 
         n = n()) %>% 
  dplyr::select(speakerid,tone,n) %>% 
  unique()

prior_ppl$tone_mean <- mean(prior$tone,na.rm=T)
prior_ppl$tone_var <- var(prior$tone,na.rm=T)
prior_ppl$tone_speaker_var <- var(prior_ppl$tone,na.rm=T)
prior_ppl$tone_weighted <- ((prior_ppl$n/prior_ppl$tone_var)*(prior_ppl$tone) + 
                           (1/prior_ppl$tone_speaker_var)*(prior_ppl$tone_mean)) / 
                           ((prior_ppl$n/prior_ppl$tone_var) +  
                              (1/prior_ppl$tone_speaker_var))

prior_ppl <- prior_ppl %>%
  dplyr::select(speakerid,avg_tone=tone,tone_weighted)

# -------------------------------------------------- #
#Part 3: Merge with VoteView Data
#and incorporate military background data 
# -------------------------------------------------- #

prior_ppl <- merge(prior_ppl, key, by = "speakerid", all.x = T, all.y = F)
prior_ppl$icpsr[prior_ppl$speakerid==107114830] <- 89767 #Goode (ICPSR id change)                       
auth_prior <- merge(auth, prior_ppl, by = "icpsr", all.x = T, all.y = F)

# Merge Background Data 

meta107 <- meta %>% filter(cong == 107) 
members <- merge(meta107, key, by = "speakerid", all.x = T, all.y = F)

members$speakerid <- NULL
members$icpsr[members$lastname=="GOODE"] <- 89767 #Switched ICPSR ID btw speech and vote

auth_prior <- merge(auth_prior, members, by = "icpsr", all.x = T, all.y = F) 

# -------------------------------------------------- #
#Part 4: Regression
# -------------------------------------------------- #

auth_prior$auth_yea[is.na(auth_prior$auth_yea)] <- 0
auth_prior$auth_nay[is.na(auth_prior$auth_nay)] <- 0
auth_prior$auth_abs <- ifelse(auth_prior$auth_yea==0 & auth_prior$auth_nay==0,1,0)
auth_clean <- auth_prior %>% filter(!auth_abs == 1) #removing absent 
auth_clean$auth_dum <- ifelse(auth_clean$auth_yea == 1, 1, 0)
auth_clean$vet <- ifelse(auth_clean$military_collapse>0,1,0)
auth_clean$rep <- ifelse(auth_clean$party=="Republican",1,0)
auth_clean$women <- ifelse(auth_clean$gender=="F",1,0)
auth_clean$senate <- ifelse(auth_clean$chamber=="S",1,0)

#### Table 1 (Main Text)
mod1 <- glm(auth_dum ~ tone_weighted, family = "binomial", data = auth_clean)
mod2 <- glm(auth_dum ~ rep, family = "binomial", data = auth_clean[!is.na(auth_clean$tone_weighted),])
mod3 <- glm(auth_dum ~ tone_weighted + rep, family = "binomial", data = auth_clean)
mod4 <- glm(auth_dum ~ tone_weighted + rep + vet + women + senate, family = "binomial", data = auth_clean)

auc(auth_clean$auth_dum, predict(mod1, auth_clean, type="response"))
auc(auth_clean$auth_dum, predict(mod2, auth_clean, type="response"))
auc(auth_clean$auth_dum, predict(mod3, auth_clean, type="response"))
auc(auth_clean$auth_dum, predict(mod4, auth_clean, type="response"))

stargazer(mod1, mod2, mod3, mod4, type = "text")



############################# Additional Predictive Validity Tests of Sentiment Scores (Appendix E) ----

############################# 2. House Resolution 557: 2004-03-17. Liberation of the Iraqi (Table A15) ----

# -------------------------------------------------- #
#Part 1: Load Vote Choice Data 
#Source: VoteView (https://voteview.com/rollcall/RH1080738)
# -------------------------------------------------- #
voteview2 <- read_xls("votechoice/iraq_bill_HRES557.xls")

# -------------------------------------------------- #
#Part 2: Sort Speech Data 
#Prior 6 months to the vote
#HRES557 voted on 2004-03-17
# -------------------------------------------------- #

####
#2-1: Filter Speech
####
prior2 <- speech %>% filter(date >= as.Date("2003-09-17") & date < as.Date("2004-03-17"))

####
#2-2: Average Tone by Members 
####
prior_ppl2 <- prior2 %>% 
  group_by(speakerid) %>% 
  mutate(tone = mean(tone,na.rm=T), 
         n = n()) %>% 
  dplyr::select(speakerid,tone,n) %>% 
  unique()
prior_ppl2$tone_mean <- mean(prior2$tone,na.rm=T)
prior_ppl2$tone_var <- var(prior2$tone,na.rm=T)
prior_ppl2$tone_speaker_var <- var(prior_ppl2$tone,na.rm=T)
prior_ppl2$tone_weighted <- ((prior_ppl2$n/prior_ppl2$tone_var)*(prior_ppl2$tone) + (1/prior_ppl2$tone_speaker_var)*(prior_ppl2$tone_mean)) / 
  ((prior_ppl2$n/prior_ppl2$tone_var) +  (1/prior_ppl2$tone_speaker_var))
prior_ppl2 <- prior_ppl2 %>%
  dplyr::select(speakerid,avg_tone=tone,tone_weighted)

# -------------------------------------------------- #
#Part 3: Merge with VoteView Data
#and incorporate military background data 
# -------------------------------------------------- #

prior_ppl2 <- merge(prior_ppl2, key, by = "speakerid", all.x = T, all.y = F)
voteview2$id <- NULL
colnames(voteview2)[4] <- "votechoice"
voteview2$auth_yea <- ifelse(voteview2$votechoice==1,1,0)
voteview2$auth_nay <- ifelse(voteview2$votechoice==6,1,0)
voteview_prior2 <- merge(voteview2, prior_ppl2, by = "icpsr", all.x = T, all.y = F)

# Merge Background Data 
meta108 <- meta %>% filter(session == 108 & chamber == "H") 
members108 <- merge(meta108, key, by = "speakerid", all.x = T, all.y = F)
members108$speakerid <- NULL
voteview_prior2 <- merge(voteview_prior2, members108, by = "icpsr", all.x = T, all.y = F) 
voteview_prior2 <- voteview_prior2 %>% unique()

# -------------------------------------------------- #
#Part 4: Regression
# -------------------------------------------------- #
voteview_prior2$auth_yea[is.na(voteview_prior2$auth_yea)] <- 0
voteview_prior2$auth_nay[is.na(voteview_prior2$auth_nay)] <- 0
voteview_prior2$auth_abs <- ifelse(voteview_prior2$auth_yea==0 & voteview_prior2$auth_nay==0,1,0)
voteview_clean2 <- voteview_prior2 %>% filter(!auth_abs == 1) #removing absent 
voteview_clean2$auth_dum <- ifelse(voteview_clean2$auth_yea == 1, 1, 0)
voteview_clean2$vet <- ifelse(voteview_clean2$military>0,1,0)
voteview_clean2$rep <- ifelse(voteview_clean2$party=="Republican",1,0)
voteview_clean2$female <- ifelse(voteview_clean2$gender=="F",1,0)

voteview_clean2$rep[voteview_clean2$icpsr==20327] <- 0 # Rodney Alexander (party change)                       
voteview_clean2$party[voteview_clean2$icpsr==20327] <- "Democrat"                  
voteview_clean2$rep[voteview_clean2$icpsr==20330] <- 0 #Chris Van Hollen (party change)                                  
voteview_clean2$party[voteview_clean2$icpsr==20330] <- "Democrat"     

mod1 <- glm(auth_dum ~ tone_weighted, family = "binomial", data = voteview_clean2)
mod2 <- glm(auth_dum ~ rep, family = "binomial", data = voteview_clean2[!is.na(voteview_clean2$tone_weighted),])
mod3 <- glm(auth_dum ~ tone_weighted + rep, family = "binomial", data = voteview_clean2)
mod4 <- glm(auth_dum ~ tone_weighted + rep + vet + female, family = "binomial", data = voteview_clean2)

####Table A15
stargazer(mod1, mod2, mod3, mod4, type = "text")
auc(voteview_clean2$auth_dum, predict(mod1, voteview_clean2, type="response"))
auc(voteview_clean2$auth_dum, predict(mod2, voteview_clean2, type="response"))
auc(voteview_clean2$auth_dum, predict(mod3, voteview_clean2, type="response"))
auc(voteview_clean2$auth_dum, predict(mod4, voteview_clean2, type="response"))



############################# 3. House Resolution 861: 2006-06-16. Declaring Global War on Terror (Table A17) ----

# -------------------------------------------------- #
#Part 1: Load Vote Choice Data 
#Source: VoteView (https://voteview.com/rollcall/RH1090956)
# -------------------------------------------------- #
voteview3 <- read_xls("votechoice/iraq_bill_HRES861.xls")

# -------------------------------------------------- #
#Part 2: Sort Speech Data 
#Prior 6 months to the vote
#HRES861 voted on 2006-06-16
# -------------------------------------------------- #

####
#2-1: Filter Speech
####
prior3 <- speech %>% filter(date >= as.Date("2006-01-03") & date < as.Date("2006-06-16")) #since starting session 

####
#2-2: Average Tones by Members 
####
prior_ppl3 <- prior3 %>% 
  group_by(speakerid) %>% 
  mutate(tone = mean(tone,na.rm=T), 
         n = n()) %>% 
  dplyr::select(speakerid,tone,n) %>% 
  unique()
prior_ppl3$tone_mean <- mean(prior3$tone,na.rm=T)
prior_ppl3$tone_var <- var(prior3$tone,na.rm=T)
prior_ppl3$tone_speaker_var <- var(prior_ppl3$tone,na.rm=T)
prior_ppl3$tone_weighted <- ((prior_ppl3$n/prior_ppl3$tone_var)*(prior_ppl3$tone) + (1/prior_ppl3$tone_speaker_var)*(prior_ppl3$tone_mean)) / 
  ((prior_ppl3$n/prior_ppl3$tone_var) +  (1/prior_ppl3$tone_speaker_var))
prior_ppl3 <- prior_ppl3 %>%
  dplyr::select(speakerid,avg_tone=tone,tone_weighted)

# -------------------------------------------------- #
#Part 3: Merge with VoteView Data
#and incorporate military background data 
# -------------------------------------------------- #
prior_ppl3 <- merge(prior_ppl3, key, by = "speakerid", all.x = T, all.y = F)
voteview3$id <- NULL
colnames(voteview3)[4] <- "votechoice"
voteview3$auth_yea <- ifelse(voteview3$votechoice==1,1,0)
voteview3$auth_nay <- ifelse(voteview3$votechoice==6,1,0)
voteview_prior3 <- merge(voteview3, prior_ppl3, by = "icpsr", all.x = T, all.y = F)

# Merge Background Data 
meta109 <- meta %>% filter(session == 109 & chamber == "H") 
members109 <- merge(meta109, key, by = "speakerid", all.x = T, all.y = F)
members109$speakerid <- NULL
voteview_prior3 <- merge(voteview_prior3, members109, by = "icpsr", all.x = T, all.y = F) 
voteview_prior3 <- voteview_prior3 %>% unique()

# -------------------------------------------------- #
#Part 4: Regression
# -------------------------------------------------- #
voteview_prior3$auth_yea[is.na(voteview_prior3$auth_yea)] <- 0
voteview_prior3$auth_nay[is.na(voteview_prior3$auth_nay)] <- 0
voteview_prior3$auth_abs <- ifelse(voteview_prior3$auth_yea==0 & voteview_prior3$auth_nay==0,1,0)
voteview_clean3 <- voteview_prior3 %>% filter(!auth_abs == 1) #removing absent 
voteview_clean3$auth_dum <- ifelse(voteview_clean3$auth_yea == 1, 1, 0)
voteview_clean3$vet <- ifelse(voteview_clean3$military>0,1,0)
voteview_clean3$rep <- ifelse(voteview_clean3$party=="Republican",1,0)
voteview_clean3$female <- ifelse(voteview_clean3$gender=="F",1,0)

mod1 <- glm(auth_dum ~ tone_weighted, family = "binomial", data = voteview_clean3)
mod2 <- glm(auth_dum ~ rep, family = "binomial", data = voteview_clean3[!is.na(voteview_clean3$tone_weighted),])
mod3 <- glm(auth_dum ~ tone_weighted + rep, family = "binomial", data = voteview_clean3)
mod4 <- glm(auth_dum ~ tone_weighted + rep + vet + female, family = "binomial", data = voteview_clean3)

####Table A17
stargazer(mod1, mod2, mod3, mod4, type = "text")
auc(voteview_clean3$auth_dum, predict(mod1, voteview_clean3, type="response"))
auc(voteview_clean3$auth_dum, predict(mod2, voteview_clean3, type="response"))
auc(voteview_clean3$auth_dum, predict(mod3, voteview_clean3, type="response"))
auc(voteview_clean3$auth_dum, predict(mod4, voteview_clean3, type="response"))

############################# 4. Senate Amendment 2087: 2007-07-18. To provide for a reduction and transition of United States forces in Iraq (Table A21) ----

# -------------------------------------------------- #
#Part 1: Load Vote Choice Data 
#Source: VoteView (https://voteview.com/rollcall/RS1100252)
# -------------------------------------------------- #
voteview5 <- read_xls("votechoice/iraq_bill_HR1585.xls")
# -------------------------------------------------- #
#Part 2: Sort Speech Data 
#Prior 6 months to the vote
#HR1585 voted on  2007-07-18
# -------------------------------------------------- #

####
#2-1: Filter Speech
####
prior5 <- speech %>% filter(date >= as.Date("2007-01-18") & date < as.Date("2007-07-18"))

####
#2-2: Average Tones by Members 
####
prior5 <- prior5 %>% filter(chamber == "S")
prior_ppl5 <- prior5 %>% 
  group_by(speakerid) %>%  
  mutate(tone = mean(tone,na.rm=T), 
         n = n()) %>% 
  dplyr::select(speakerid,tone,n) %>% 
  unique()
prior_ppl5$tone_mean <- mean(prior5$tone,na.rm=T)
prior_ppl5$tone_var <- var(prior5$tone,na.rm=T)
prior_ppl5$tone_speaker_var <- var(prior_ppl5$tone,na.rm=T)
prior_ppl5$tone_weighted <- ((prior_ppl5$n/prior_ppl5$tone_var)*(prior_ppl5$tone) + (1/prior_ppl5$tone_speaker_var)*(prior_ppl5$tone_mean)) / 
  ((prior_ppl5$n/prior_ppl5$tone_var) +  (1/prior_ppl5$tone_speaker_var))
prior_ppl5 <- prior_ppl5 %>%
  dplyr::select(speakerid,avg_tone=tone,tone_weighted)

# -------------------------------------------------- #
#Part 3: Merge with VoteView Data
#and incorporate military background data 
# -------------------------------------------------- #
prior_ppl5 <- merge(prior_ppl5, key, by = "speakerid", all.x = T, all.y = F)
# Merge voteview data
head(voteview5)
voteview5$id <- NULL
colnames(voteview5)[4] <- "votechoice"
voteview5$auth_yea <- ifelse(voteview5$votechoice==1,1,0)
voteview5$auth_nay <- ifelse(voteview5$votechoice==6,1,0)
voteview_prior5 <- merge(voteview5, prior_ppl5, by = "icpsr", all.x = T, all.y = F)

# Merge Background Data 
meta110 <- meta %>% filter(session == 110 & chamber == "S") 
members110 <- merge(meta110, key, by = "speakerid", all.x = T, all.y = F)
members110$speakerid <- NULL
voteview_prior5 <- merge(voteview_prior5, members110, by = "icpsr", all.x = T, all.y = F) 
voteview_prior5 <- voteview_prior5 %>% unique()

# -------------------------------------------------- #
#Part 4: Regression
# -------------------------------------------------- #
voteview_prior5$auth_yea[is.na(voteview_prior5$auth_yea)] <- 0
voteview_prior5$auth_nay[is.na(voteview_prior5$auth_nay)] <- 0
voteview_prior5$auth_abs <- ifelse(voteview_prior5$auth_yea==0 & voteview_prior5$auth_nay==0,1,0)
voteview_clean5 <- voteview_prior5 %>% filter(!auth_abs == 1) #removing absent 
voteview_clean5$party[voteview_clean5$icpsr==14910] <- "Republican" #Specter party change                                  
voteview_clean5$rep[voteview_clean5$icpsr==14910] <- 1
voteview_clean5$auth_dum <- ifelse(voteview_clean5$auth_yea == 1, 1, 0)
voteview_clean5$vet <- ifelse(voteview_clean5$military>0,1,0)
voteview_clean5$rep <- ifelse(voteview_clean5$party=="Republican",1,0)
voteview_clean5$female <- ifelse(voteview_clean5$gender=="F",1,0)

mod1 <- glm(auth_dum ~ tone_weighted, family = "binomial", data = voteview_clean5)
mod2 <- glm(auth_dum ~ rep, family = "binomial", data = voteview_clean5[!is.na(voteview_clean5$tone_weighted),])
mod3 <- glm(auth_dum ~ tone_weighted + rep, family = "binomial", data = voteview_clean5)
mod4 <- glm(auth_dum ~ tone_weighted + rep + vet + female, family = "binomial", data = voteview_clean5)

####Table A21
stargazer(mod1, mod2, mod3, mod4, type = "text")
auc(voteview_clean5$auth_dum, predict(mod1, voteview_clean5, type="response"))
auc(voteview_clean5$auth_dum, predict(mod2, voteview_clean5, type="response"))
auc(voteview_clean5$auth_dum, predict(mod3, voteview_clean5, type="response"))
auc(voteview_clean5$auth_dum, predict(mod4, voteview_clean5, type="response"))


############################# 5. House Resolution 2956: 2007-07-12. Providing for consideration of H.R. 2956, to require the Secretary of Defense to commence the reduction of the number of United States Armed Forces in Iraq to a limited presence by April 1, 2008, and for other purposes (Table A20) ----

# -------------------------------------------------- #
#Part 1: Load Vote Choice Data 
#Source: VoteView (https://voteview.com/rollcall/RH1100614)
# -------------------------------------------------- #
voteview6 <- read_xls("votechoice/iraq_bill_HRES533.xls")

# -------------------------------------------------- #
#Part 2: Sort Speech Data 
#Prior 6 months to the vote
#HR1585 voted on  2007-07-12
# -------------------------------------------------- #
####
#2-1: Filter Speech
####
prior6 <- speech %>% filter(date >= as.Date("2007-01-12") & date < as.Date("2007-07-12"))

####
#2-2: Average Tone by Members 
####
prior6 <- prior6 %>% filter(chamber == "H")
prior_ppl6 <- prior6 %>% 
  group_by(speakerid) %>%  
  mutate(tone = mean(tone,na.rm=T), 
         n = n()) %>% 
  dplyr::select(speakerid,tone,n) %>% 
  unique()
prior_ppl6$tone_mean <- mean(prior6$tone,na.rm=T)
prior_ppl6$tone_var <- var(prior6$tone,na.rm=T)
prior_ppl6$tone_speaker_var <- var(prior_ppl6$tone,na.rm=T)
prior_ppl6$tone_weighted <- ((prior_ppl6$n/prior_ppl6$tone_var)*(prior_ppl6$tone) + (1/prior_ppl6$tone_speaker_var)*(prior_ppl6$tone_mean)) / 
  ((prior_ppl6$n/prior_ppl6$tone_var) +  (1/prior_ppl6$tone_speaker_var))
prior_ppl6 <- prior_ppl6 %>%
  dplyr::select(speakerid,avg_tone=tone,tone_weighted)

# -------------------------------------------------- #
#Part 3: Merge with VoteView Data
#and incorporate military background data 
# -------------------------------------------------- #
prior_ppl6 <- merge(prior_ppl6, key, by = "speakerid", all.x = T, all.y = F)
# Merge voteview data
head(voteview6)
voteview6$id <- NULL
colnames(voteview6)[4] <- "votechoice"
voteview6$auth_yea <- ifelse(voteview6$votechoice==1,1,0)
voteview6$auth_nay <- ifelse(voteview6$votechoice==6,1,0)
voteview_prior6 <- merge(voteview6, prior_ppl6, by = "icpsr", all.x = T, all.y = F)

# Merge Background Data 
meta110 <- meta %>% filter(session == 110 & chamber == "H") 
members110 <- merge(meta110, key, by = "speakerid", all.x = T, all.y = F)
members110$speakerid <- NULL
voteview_prior6 <- merge(voteview_prior6, members110, by = "icpsr", all.x = T, all.y = F) 
voteview_prior6 <- voteview_prior6 %>% unique()

# -------------------------------------------------- #
#Part 4: Regression
# -------------------------------------------------- #
voteview_prior6$auth_yea[is.na(voteview_prior6$auth_yea)] <- 0
voteview_prior6$auth_nay[is.na(voteview_prior6$auth_nay)] <- 0
voteview_prior6$auth_abs <- ifelse(voteview_prior6$auth_yea==0 & voteview_prior6$auth_nay==0,1,0)
voteview_clean6 <- voteview_prior6 %>% filter(!auth_abs == 1) #removing absent 
voteview_clean6$auth_dum <- ifelse(voteview_clean6$auth_yea == 1, 1, 0)
voteview_clean6$vet <- ifelse(voteview_clean6$military>0,1,0)
voteview_clean6$rep <- ifelse(voteview_clean6$party=="Republican",1,0)
voteview_clean6$female <- ifelse(voteview_clean6$gender=="F",1,0)

mod1 <- glm(auth_dum ~ tone_weighted, family = "binomial", data = voteview_clean6)
mod2 <- glm(auth_dum ~ rep, family = "binomial", data = voteview_clean6[!is.na(voteview_clean6$tone_weighted),])
mod3 <- glm(auth_dum ~ tone_weighted + rep, family = "binomial", data = voteview_clean6)
mod4 <- glm(auth_dum ~ tone_weighted + rep + vet + female, family = "binomial", data = voteview_clean6)

####Table A20
stargazer(mod1, mod2, mod3, mod4, type = "text")
auc(voteview_clean6$auth_dum, predict(mod1, voteview_clean6, type="response"))
auc(voteview_clean6$auth_dum, predict(mod2, voteview_clean6, type="response"))
auc(voteview_clean6$auth_dum, predict(mod3, voteview_clean6, type="response"))
auc(voteview_clean6$auth_dum, predict(mod4, voteview_clean6, type="response"))




#############################
############################# Votes on Resourcing Conflict Provisions (Appendix N) ----
#############################
############################# 6. House Resolution 3289: Emergency Supplemental Appropriations for Iraq and Afghanistan Security and Reconstruction Act, 2004 (Table A42) ----

# -------------------------------------------------- #
#Part 1: Load Vote Choice Data 
#Source: VoteView (https://voteview.com/rollcall/RH1080560)
# -------------------------------------------------- #
voteview7 <- read_xls("votechoice/iraq_bill_HR3289.xls")
# -------------------------------------------------- #
#Part 2: Sort Speech Data 
#Prior 6 months to the vote
#HR3289 voted on 2003-10-17
#Result: 303-125 (Passed)
# -------------------------------------------------- #
####
#2-1: Filter Speech
####
prior7 <- speech %>% filter(date >= as.Date("2003-04-17") & date < as.Date("2003-10-17"))
####
#2-2: Average Tones by Members 
####
prior_ppl7 <- prior7 %>% 
  group_by(speakerid) %>% 
  mutate(tone = mean(tone,na.rm=T), 
         n = n()) %>% 
  dplyr::select(speakerid,tone,n) %>% 
  unique()
prior_ppl7$tone_mean <- mean(prior7$tone,na.rm=T)
prior_ppl7$tone_var <- var(prior7$tone,na.rm=T)
prior_ppl7$tone_speaker_var <- var(prior_ppl7$tone,na.rm=T)
prior_ppl7$tone_weighted <- ((prior_ppl7$n/prior_ppl7$tone_var)*(prior_ppl7$tone) + (1/prior_ppl7$tone_speaker_var)*(prior_ppl7$tone_mean)) / 
  ((prior_ppl7$n/prior_ppl7$tone_var) +  (1/prior_ppl7$tone_speaker_var))
prior_ppl7 <- prior_ppl7 %>%
  dplyr::select(speakerid,avg_tone=tone,tone_weighted)

# -------------------------------------------------- #
#Part 3: Merge with VoteView Data
#and incorporate military background data 
# -------------------------------------------------- #
prior_ppl7 <- merge(prior_ppl7, key, by = "speakerid", all.x = T, all.y = F)
# Merge voteview data
head(voteview7)
voteview7$id <- NULL
colnames(voteview7)[4] <- "votechoice"
voteview7$auth_yea <- ifelse(voteview7$votechoice==1,1,0)
voteview7$auth_nay <- ifelse(voteview7$votechoice==6,1,0)
voteview_prior7 <- merge(voteview7, prior_ppl7, by = "icpsr", all.x = T, all.y = F)

# Merge Background Data 
meta108 <- meta %>% filter(cong == 108 & chamber == "H") 
members108 <- merge(meta108, key, by = "speakerid", all.x = T, all.y = F)
members108$icpsr[members108$icpsr==94828] <- 14828 # Switching Hall ICPSR number for correspondence
members108$icpsr[members108$speakerid==108121620] <- 20330 #Van Hollen
members108$speakerid <- NULL
voteview_prior7 <- merge(voteview_prior7, members108, by = "icpsr", all.x = T, all.y = F) 
voteview_prior7 <- voteview_prior7 %>% unique()


# -------------------------------------------------- #
#Part 4: Regression
# -------------------------------------------------- #
voteview_prior7$auth_yea[is.na(voteview_prior7$auth_yea)] <- 0
voteview_prior7$auth_nay[is.na(voteview_prior7$auth_nay)] <- 0
voteview_prior7$auth_abs <- ifelse(voteview_prior7$auth_yea==0 & voteview_prior7$auth_nay==0,1,0)
voteview_clean7 <- voteview_prior7 %>% filter(!auth_abs == 1) #removing absent 
voteview_clean7$auth_dum <- ifelse(voteview_clean7$auth_yea == 1, 1, 0)
voteview_clean7$vet <- ifelse(voteview_clean7$military_collapse>0,1,0)
voteview_clean7$rep <- ifelse(voteview_clean7$party=="Republican",1,0)
voteview_clean7$party[voteview_clean7$icpsr==14828] <- 'Democrat' # Hall
voteview_clean7$rep[voteview_clean7$icpsr==14828] <- 0 # Hall
voteview_clean7$party[voteview_clean7$icpsr==20327] <- 'Democrat' # ALEXANDER
voteview_clean7$rep[voteview_clean7$icpsr==20327] <- 0 # ALEXANDER 
voteview_clean7$female <- ifelse(voteview_clean7$gender=="F",1,0)

mod1 <- glm(auth_dum ~ vet, family = "binomial", data = voteview_clean7)
mod2 <- glm(auth_dum ~ rep, family = "binomial", data = voteview_clean7)
mod3 <- glm(auth_dum ~ vet + rep + female, family = "binomial", data = voteview_clean7)

####Table A40
stargazer(mod1, mod2, mod3, type="text")
auc(voteview_clean7$auth_dum, predict(mod1, voteview_clean7, type="response"))
auc(voteview_clean7$auth_dum, predict(mod2, voteview_clean7, type="response"))
auc(voteview_clean7$auth_dum, predict(mod3, voteview_clean7, type="response"))


############################# 7. House Resolution 4939: Emergency Supplemental Appropriations Act for Defense, the Global War on Terror, and Hurricane Recovery, 2006 (Table A44) ----
# -------------------------------------------------- #
#Part 1: Load Vote Choice Data 
#Source: VoteView (https://voteview.com/rollcall/RH1090733)
# -------------------------------------------------- #
voteview9 <- read_xls("votechoice/iraq_bill_HR4939.xls")
# -------------------------------------------------- #
#Part 2: Sort Speech Data 
#Prior 6 months to the vote
#HR4939 voted on 2006-03-16
#Result: 349 - 71 (Passed)
# -------------------------------------------------- #
####
#2-1: Filter Speech
####
prior9 <- speech %>% filter(date >= as.Date("2005-09-16") & date < as.Date("2006-03-16"))
####
#2-2: Average Tones by Members 
####
prior_ppl9 <- prior9 %>% 
  group_by(speakerid) %>% 
  mutate(tone = mean(tone,na.rm=T), 
         n = n()) %>% 
  dplyr::select(speakerid,tone,n) %>% 
  unique()
prior_ppl9$tone_mean <- mean(prior9$tone,na.rm=T)
prior_ppl9$tone_var <- var(prior9$tone,na.rm=T)
prior_ppl9$tone_speaker_var <- var(prior_ppl9$tone,na.rm=T)
prior_ppl9$tone_weighted <- ((prior_ppl9$n/prior_ppl9$tone_var)*(prior_ppl9$tone) + (1/prior_ppl9$tone_speaker_var)*(prior_ppl9$tone_mean)) / 
  ((prior_ppl9$n/prior_ppl9$tone_var) +  (1/prior_ppl9$tone_speaker_var))
prior_ppl9 <- prior_ppl9 %>%
  dplyr::select(speakerid,avg_tone=tone,tone_weighted)
# -------------------------------------------------- #
#Part 3: Merge with VoteView Data
#and incorporate military background data 
# -------------------------------------------------- #
prior_ppl9 <- merge(prior_ppl9, key, by = "speakerid", all.x = T, all.y = F)
# Merge voteview data
voteview9$id <- NULL
colnames(voteview9)[4] <- "votechoice"
voteview9$auth_yea <- ifelse(voteview9$votechoice==1,1,0)
voteview9$auth_nay <- ifelse(voteview9$votechoice==6,1,0)
voteview_prior9 <- merge(voteview9, prior_ppl9, by = "icpsr", all.x = T, all.y = F)

# Merge Background Data 
meta109 <- meta %>% filter(cong == 109 & chamber == "H") 
members109 <- merge(meta109, key, by = "speakerid", all.x = T, all.y = F)
members109$speakerid <- NULL
voteview_prior9 <- merge(voteview_prior9, members109, by = "icpsr", all.x = T, all.y = F) 
voteview_prior9 <- voteview_prior9 %>% unique()
sp_109 <- unique(voteview_prior9$icpsr) 


# -------------------------------------------------- #
#Part 4: Regression
# -------------------------------------------------- #
voteview_prior9$auth_yea[is.na(voteview_prior9$auth_yea)] <- 0
voteview_prior9$auth_nay[is.na(voteview_prior9$auth_nay)] <- 0
voteview_prior9$auth_abs <- ifelse(voteview_prior9$auth_yea==0 & voteview_prior9$auth_nay==0,1,0)
voteview_clean9 <- voteview_prior9 %>% filter(!auth_abs == 1) #removing absent 
voteview_clean9$auth_dum <- ifelse(voteview_clean9$auth_yea == 1, 1, 0)
voteview_clean9$vet <- ifelse(voteview_clean9$military_collapse>0,1,0)
voteview_clean9$rep <- ifelse(voteview_clean9$party=="Republican",1,0)
voteview_clean9$female <- ifelse(voteview_clean9$gender=="F",1,0)


mod1 <- glm(auth_dum ~ vet, family = "binomial", data = voteview_clean9)
mod2 <- glm(auth_dum ~ rep, family = "binomial", data = voteview_clean9)
mod3 <- glm(auth_dum ~ vet + rep + female, family = "binomial", data = voteview_clean9)

####Table A42
stargazer(mod1, mod2, mod3, type = "text")
auc(voteview_clean9$auth_dum, predict(mod1, voteview_clean9, type="response"))
auc(voteview_clean9$auth_dum, predict(mod2, voteview_clean9, type="response"))
auc(voteview_clean9$auth_dum, predict(mod3, voteview_clean9, type="response"))







